package bd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import logic.User;

import bd.JDBC;
/**
 * 
 * 
 *
 */
public class JDBCuser implements JDBC{

	private static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";
	private static final String CONNECTION_URL = "jdbc:sqlite:DB/reversi.s3db";
	
	private Connection connection;
	private ResultSet resulset;
	
	public JDBCuser(){
		try {
			Class.forName(DRIVER_CLASS_NAME);
			connection= DriverManager.getConnection(CONNECTION_URL);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void close() throws SQLException{
		connection.close();
	}
	
	@Override
	public void insert(Object o) throws SQLException, ClassNotFoundException {
		if(o instanceof User){
			String sql="";
			User v=(User)o;
			sql= "INSERT INTO user VALUES(?,?,?,?,?);";
			
			PreparedStatement pstmt = connection.prepareStatement( sql );
			pstmt.setString( 1, v.getNick()); 
			pstmt.setString( 2, v.getPass()); 
			pstmt.setLong( 3, v.getVictorias()); 
			pstmt.setLong( 4, v.getDerrotas());
			pstmt.setLong( 5, v.getEmpates());
			pstmt.execute();
			
			pstmt.close();
		}else 
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo User!");
	}

	@Override
	public void delete(Object o) throws SQLException {
		if(o instanceof User){
			String sql="";
			User v=(User)o;
			sql= "DELETE FROM User WHERE nick=(?);";
			PreparedStatement pstmt = connection.prepareStatement( sql );
			pstmt.setString( 1, v.getNick());
			pstmt.execute();
			pstmt.close();
			
		}else
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo User!");
	}

	@Override
	public ArrayList<User> selectAll() throws SQLException {
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		sql= "Select * FROM user;";
		resulset=statement.executeQuery(sql);
		ArrayList<User> lista= new ArrayList<User>();
		while(resulset.next()){
			lista.add(convertUser(resulset));
		}
		statement.close();
		return lista;
	}

	@Override
	public User selectOne(Object o) throws SQLException, IllegalArgumentException{		
		if(o instanceof String){
			User user= null;
			String clave=(String) o;
			String sql="";
			sql= "Select * FROM user WHERE nick=?;";
						
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, clave);
			resulset=pstmt.executeQuery();
			
			if(resulset.next()){
				user= convertUser(resulset);
			}
			pstmt.close();
			if(user==null){
				return null;
			}
			return user;
		}
		//si el objeto no es del tipo requerido se lanzara la siguiente excepcion.
		else throw new IllegalArgumentException("El parametro recibido debe ser de tipo String!");
	}

	@Override
	public void update(Object o) throws SQLException, ClassNotFoundException {
		delete(o);
		insert(o);
	}
	
	/**
	 * Metodo que crea a partir de un resultSet un objetoUsuario
	 * @param res ResultSet a partir del cual crear un usuario
	 * @return Usuario creado
	 * @throws SQLException
	 */
	private User convertUser(ResultSet res) throws SQLException{
		User usuario=new User();
		
		usuario.setNick(res.getString("nick"));
		
		usuario.setPass(res.getString("pass"));
		usuario.setVictorias(res.getInt("victorias"));
		usuario.setDerrotas(res.getInt("derrotas"));
		usuario.setEmpates(res.getInt("empates"));
		return usuario;
	}
}
